package msg.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;

import msg.action.ClientAction;
import msg.model.AppClient;


public class ClientView extends JFrame {

	private static final long serialVersionUID = 1L;
	private String appName;
	private AppClient appClient;
	private JToggleButton butConnect;
	private JTextField txtUser;
	private JTextField txtTO;
	private JTextField txtIP;
	private JTextArea txtBox;
	private JTextArea txtMsg;
	private JButton butMsg;
	private JLabel labState;
	private JLabel labTO;

	/** constructor arguments: program name, application class */
	public ClientView(String appName) {

		//setting:
		super(appName);
		this.setVisible(true);
		this.setResizable(true);

		//references:
		this.appClient = new AppClient(this);
		this.appName = appName;
		

		//components:
		this.initView();
		this.pack();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
	}
	
	/** program view initiation */
	private void initView() {

		//elements:
		this.butConnect = new JToggleButton("Connect");
		this.txtUser = new JTextField("name", 6);
		this.txtTO = new JTextField("to", 6);
		this.txtIP = new JTextField("", 8);

		this.txtBox = new JTextArea(18, 20);
		this.txtMsg = new JTextArea(2, 22);
		this.butMsg = new JButton("Send");
		this.labState = new JLabel(" Loading ");
		this.labTO = new JLabel(">>");

		JScrollPane listBox = new JScrollPane(txtBox);
		JScrollPane listSend = new JScrollPane(txtMsg);


		//settings:
		this.butConnect.addActionListener(new ClientAction("butConnect", this));
		this.butMsg.addActionListener(new ClientAction("butMsg", this));
		
		this.txtUser.setHorizontalAlignment(JTextField.RIGHT);
		this.txtUser.addKeyListener(new ClientAction("txtUser", this));
		
		this.txtTO.setHorizontalAlignment(JTextField.RIGHT);
		this.txtTO.addKeyListener(new ClientAction("txtTO", this));

		this.txtIP.setHorizontalAlignment(JTextField.RIGHT);
		this.txtIP.addKeyListener(new ClientAction("txtIP", this));

		this.txtBox.setLineWrap(true);
		this.txtBox.setWrapStyleWord(true);
		this.txtBox.setEditable(false);

		this.txtMsg.setLineWrap(true);
		this.txtMsg.setWrapStyleWord(true);
		this.txtMsg.addKeyListener(new ClientAction("txtMsg", this));

		this.labState.setBorder(BorderFactory.createLineBorder(Color.gray));
		this.labState.setForeground(Color.gray);

		listBox.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		listBox.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		listSend.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		listSend.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);


		//JPanels:
		JPanel p0 = new JPanel(new BorderLayout());
		JPanel p1 = new JPanel(new FlowLayout(1));
		JPanel p2 = new JPanel(new FlowLayout(1));
		JPanel p3 = new JPanel(new BorderLayout());

		p1.add(this.butConnect);
		p1.add(this.txtUser);
		p1.add(this.labTO);
		p1.add(this.txtTO);
		p1.add(this.txtIP);

		p2.add(listSend);
		p2.add(this.butMsg);

		p3.add(p2, BorderLayout.NORTH);
		p3.add(this.labState, BorderLayout.SOUTH);

		p0.add(p1, BorderLayout.NORTH);
		p0.add(listBox, BorderLayout.CENTER);
		p0.add(p3, BorderLayout.SOUTH);
		add(p0);
	}

	/** method set view for connect button */
	public void viewConnect(boolean has) {
		this.butConnect.setSelected(has);
		this.txtUser.setEditable(!has);
		this.txtIP.setEditable(!has);

		this.butMsg.setEnabled(has);
		this.txtMsg.setEnabled(has);
		this.txtMsg.setText("");
	}

	/** method set view for send */
	public void viewSend() {
		this.setTxtBox(this.txtUser.getText() + " << " + this.txtMsg.getText());
		this.txtMsg.setText("");
	}

	/** method field text box box */
	public void setTxtBox(String txt) {
		this.txtBox.setText(txtBox.getText() + txt + "\n");
	}

	/** method field massage box box */
	public void setTxtMsg(String txt) {
		this.txtMsg.setText(txt);
	}

	/** method set status application */
	public void setState(String state) {
		this.labState.setText(" Status: " + state);
	}

	/** method set default view for application */
	public void setDefault() {
		this.viewConnect(false);
		this.txtIP.setText(this.appClient.getLocalIP());
		this.setState("Client ready to use.");
	}
	
	/** method get AppClient class */
	public AppClient getAppClient() {
		return this.appClient;
	}

	/** method get text from txtMsg */
	public String getTxtMsg() {
		return this.txtMsg.getText();
	}

	/** method get text from txtTO */
	public String getTxtTO() {
		return this.txtTO.getText();
	}

	/** method get text from txtIP */
	public String getTxtIP() {
		return this.txtIP.getText();
	}

	/** method get text from txtUser */
	public String getTxtUser() {
		return this.txtUser.getText();
	}

	/** method get status from butConnect */
	public boolean getButConnect() {
		return this.butConnect.isSelected();
	}

	/** method view message box */
	public void messageBox(String text, int typ) {
		try {
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(this, text, appName, typ);

		} catch (Exception ex) {
			System.err.println("Message Dialog error!");
			System.err.println("Error message: " + ex.toString());
		}
	}
}